package com.rtx.edi.platform.synctask.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.edi.model.api.po.PlApiMapping;
import com.rtx.edi.model.api.po.PlApiMappingStructure;
import com.rtx.edi.model.log.po.PlApiRequestLog;
import com.rtx.edi.platform.baseinfo.service.PlApiService;
import com.rtx.edi.platform.handler.JsonConvert;
import com.rtx.edi.platform.handler.TargetFieldHandle;
import com.rtx.edi.platform.handler.service.HandleSqlService;
import com.rtx.edi.platform.log.service.PlApiRequestContentLogService;
import com.rtx.edi.platform.synctask.handler.PlTaskHandler;
import com.rtx.edi.platform.synctask.service.ConvertService;

import cn.hutool.json.JSONObject;

@Component
public class ConvertServiceImpl implements ConvertService{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private PlApiService plApiService;

	@Autowired
	PlApiRequestContentLogService plApiRequestContentLogService;
	
	@Autowired
	HandleSqlService handleSqlService;
	
	@Autowired
	PlTaskHandler plTaskHandler;

	/**
	 * 通过配置转换目标json 
	 */
	@Override
	public String ConvertByMappingCodeAndSource(String method, String body) {

		String targetjson = null;
		
		try {
			//转化为JSON
			JSONObject sourceJson = plTaskHandler.getJSONObject(body);
			
			PlApiMapping plApiMapping = plApiService.getPlApiMappingByApiMappingCode(method);
			Assert.notNull(plApiMapping,"对接场景编号：" + method + "不存在");
			//获取场景映射信息
			ApiMappingInfoBo apiInfo = plApiService.getApiMappingInfo(plApiMapping.getCompanyId(),plApiMapping.getId(),new PlApiRequestLog());
			//获取报文结构
			List<PlApiMappingStructure> structureList = plApiService.getPlApiMappingStructure(apiInfo.getApiMappingId());
			//转化报文结构
			JsonConvert convert = new JsonConvert(sourceJson.toString(),apiInfo.getTargetJsonTemplate(),structureList,null);
			convert.convert();
			
			targetjson = convert.getTargetJosn();
			
			//处理字段 配置得转换脚本
			targetjson = TargetFieldHandle.handleFieldScript(targetjson, structureList);
			
			//处理字段 设置默认值
			targetjson = TargetFieldHandle.handleTargetDefaultValue(targetjson, structureList);
			
		} catch (Exception e) {
			logger.error("请求异常-",e);
			targetjson = e.getMessage();
		}
		
		//记录日志
		return targetjson;
	
	}
	
	

	@Override
	public String ConvertByMappingCodeAndSource(ApiMappingInfoBo apiInfo, String body) {
		String targetjson = null;
		
		try {
			//转化为JSON
			JSONObject sourceJson = plTaskHandler.getJSONObject(body);
			//获取报文结构
			List<PlApiMappingStructure> structureList = plApiService.getPlApiMappingStructure(apiInfo.getApiMappingId());
			//转化报文结构
			JsonConvert convert = new JsonConvert(sourceJson.toString(),apiInfo.getTargetJsonTemplate(),structureList,null);
			convert.convert();
			
			targetjson = convert.getTargetJosn();
			
			//处理字段 配置得转换脚本
			targetjson = TargetFieldHandle.handleFieldScript(targetjson, structureList);
			
			//处理字段 设置默认值
			targetjson = TargetFieldHandle.handleTargetDefaultValue(targetjson, structureList);
			
		} catch (Exception e) {
			logger.error("请求异常-",e);
			targetjson = e.getMessage();
		}
		
		//记录日志
		return targetjson;
	}



	@Override
	public ApiMappingInfoBo getConfigByApimappingcode(String method) {
		PlApiMapping plApiMapping = plApiService.getPlApiMappingByApiMappingCode(method);
		Assert.notNull(plApiMapping,"对接场景编号：" + method + "不存在");
		//获取场景映射信息
		ApiMappingInfoBo apiInfo = plApiService.getApiMappingInfo(plApiMapping.getCompanyId(),plApiMapping.getId(),new PlApiRequestLog());
		return apiInfo;
	}
}
